<?php if(!defined('BASEPATH')) exit("Sorry, what you're looking not found.");
class C_prestasi extends Nvl_Controller{
    function __construct(){
        parent::__construct();
        session_start();
		$this->__checkSession();
        $this->load->model('m_prestasi');
    }

    function index(){
        $this->load->view('pages/v_prestasi');
    }

    function get_list(){
        $pagenum = $this->input->numeric('pagenum', 'get');
        $pagesize = $this->input->numeric('pagesize', 'get');
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;
		$search_text = $this->input->get('search_text');
		$result = $this->m_prestasi->get_list($start, $end, $search_text);
		echo $result;
    }
	
	function get_list_siswa(){
        $pagenum = $this->input->get('pagenum');
        $pagesize = $this->input->get('pagesize');
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;
		$search_text = $this->input->get('search_text');
		
		$DB = $this->switch_db();
		$result = $this->m_prestasi->get_list_siswa($start, $end, $search_text, $DB);
		echo $result;
    }
	
	function get_list_detail(){
		$prestasi_id = $this->input->get('prestasi_id');
		$result = $this->m_prestasi->get_list_detail($prestasi_id);
		echo $result;
	}
	
	function save(){
		$params = json_decode($this->input->post("params"));
		$form_data = extract(get_object_vars($params->form_data));
		
		$prestasi_data = array(
			'prestasi_lomba_id'=>$prestasi_lomba_id,
			'prestasi_sekolah_id'=>$_SESSION[SESSION_SCHOOLID],
			'prestasi_tanggal'=>help_en_date($prestasi_tanggal),
			'prestasi_create_by'=>$_SESSION[SESSION_USERID],
			'prestasi_create_date'=>date('Y-m-d H:i:s'),
			'prestasi_sekolah_nama'=>$_SESSION[SESSION_SCHOOLNAME]
		);
		if(help_numeric($prestasi_juara_id) != 0){
			$skor = $this->m_prestasi->get_list_juara_poin($prestasi_lomba_id, '', $prestasi_juara_id);
			$prestasi_data['prestasi_juara_id']=$prestasi_juara_id;
			$prestasi_data['prestasi_skor']=$skor;
		}
		
		$result = $this->m_prestasi->__save($prestasi_data, $params->prestasi_id, FALSE, TRUE);
		
		$this->db->where('det_prestasi_prestasi_id', $result)->delete('t_prestasi_det');
		$data_detail = array();
		
		foreach($params->detail as $sub){
			if($sub->det_prestasi_siswa_id != ''){
				$data_detail[] = array(
					'det_prestasi_siswa_id'=>$sub->det_prestasi_siswa_id,
					'det_prestasi_siswa_nama'=>$sub->det_prestasi_siswa_nama,
					'det_prestasi_siswa_kelas'=>$sub->det_prestasi_siswa_kelas,
					'det_prestasi_siswa_alamat'=>$sub->det_prestasi_siswa_alamat,
					'det_prestasi_siswa_tgllahir'=>$sub->det_prestasi_siswa_tgllahir,
					'det_prestasi_prestasi_id'=>$result
				);
			}
		}
		$this->db->insert_batch('t_prestasi_det', $data_detail);
		echo $result;
	}
	
	function delete(){
		$prestasi_id = $this->input->numeric('prestasi_id');
		$result = $this->m_prestasi->__deactivate($prestasi_id);
		echo $result;
	}
	
	function get_list_juara_poin(){
		$lomba_id = $this->input->get('lomba_id');
		$search_text = $this->input->get('search_text');
		$result = $this->m_prestasi->get_list_juara_poin($lomba_id, $search_text);
		echo $result;
	}
}